package com.bsh.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.Admin;
import com.bsh.mybatis.model.Role;
import com.bsh.service.AdminService;
import com.bsh.service.ModuleService;
import com.bsh.service.RoleService;
import com.bsh.shiro.UserContext;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;

/**
 * @author gsh
 * @since 0.0.1
 * 
 */
@Controller
public class AdminController {
	private static Logger log = LoggerFactory.getLogger(AdminController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ModuleService module;
	@Autowired
	private AdminService adminService;
	@Autowired
	private RoleService roleService;

	/**
	 * 管理员列表
	 */
	@RequestMapping(value = "/system/admin/list")
	public ModelAndView adminList() {
		String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		String email = request.getParameter("email") == null ? null : request.getParameter("email").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = adminService.getPage(pager, userName, email);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		return mv;
	}

	/**
	 * 修改管理员--初使化
	 */
	@RequestMapping(value = "/system/admin/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer adminid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminEdit");
		Admin admin = adminService.getAdmin(adminid);
		mv.addObject("admin", admin);
		return mv;
	}

	/**
	 * 修改管理员--保存
	 */
	@RequestMapping(value = "/system/admin/edit/save")
	public ModelAndView editSave(@ModelAttribute Admin admin) {
		Integer res = adminService.editAdmin(admin);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminEdit");
		mv.addObject("admin", admin);

		String msg = PageTools.alertCloseFraRefresh("修改管理员", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 添加管理员--初使化
	 */
	@RequestMapping(value = "/system/admin/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminAdd");
		return mv;
	}

	/**
	 * 添加管理员--保存
	 */
	@RequestMapping(value = "/system/admin/add/save")
	public ModelAndView addSave(@ModelAttribute Admin admin, @RequestParam(value = "rePassword") String rePassword) {
		ModelAndView mv = new ModelAndView();
		admin.setCreateTime(new Date());

		Integer res = adminService.addAdmin(admin);
		mv.setViewName("/pages/system/adminAdd");
		mv.addObject("admin", admin);
		mv.addObject("rePassword", rePassword);

		String msg = PageTools.alertCloseFraRefresh("添加管理员", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 查看管理员信息
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/view/{id}")
	public PageJosnMessage view(@PathVariable(value = "id") Integer adminid) {
		Admin admin = adminService.getAdmin(adminid);
		PageJosnMessage msg = new PageJosnMessage();
		msg.putData("admin", admin);
		return msg;
	}

	/**
	 * 启用、禁用管理员
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/enable")
	public PageJosnMessage enable(@RequestParam int id, @RequestParam int isEnable) {
		Integer res = adminService.enableAdmin(id, isEnable == 1);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 批量禁用
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/disable/batch")
	public PageJosnMessage disableBatch(@RequestBody int[] idList) {
		Integer res = adminService.disableBatch(idList);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 批量启用
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/enable/batch")
	public PageJosnMessage enableBatch(@RequestBody int[] idList) {
		Integer res = adminService.enableBatch(idList);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/delete/{id}")
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
		Integer res = adminService.deleteAdmin(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}	
	
	/**
	 * 查看管理员角色
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/roles/{id}")
	public ModelAndView adminRolse(@PathVariable(value = "id") Integer adminid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminRoles");
		List<Role> adminRoleList = adminService.getAdminRoles(adminid);
		mv.addObject("adminRroleList", adminRoleList);	
		List<Role> roleList = roleService.getRoles();
		mv.addObject("roleList", roleList);	

		if (adminRoleList!=null) {
			List<Integer> permsList=new ArrayList<Integer>();
			for (Role role : adminRoleList) {
				permsList.add(role.getId());
			}
			Gson g=new Gson();
			mv.addObject("perms", g.toJson(permsList));
		}
		return mv;
	}
	
	/**
	 * 查看管理员角色
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/roles/save/")
	public ModelAndView adminRolseSave(@RequestParam(value = "id") Integer adminid) {
		String[] roleList=request.getParameterValues("roleId");
		roleService.setAdminRole(adminid, roleList);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/adminRoles");
		String msg = PageTools.alertCloseFraRefresh("设置管理员角色", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}
	
	
	/**
	 * 重置密码
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/password/reset/{id}")
	public PageJosnMessage resetPassword(@PathVariable(value = "id") Integer id) {
		Integer res = adminService.resetPassword(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}
	
	/**
	 * 修改密码--初使化
	 */
	@RequestMapping(value = "/system/admin/password/change")
	public ModelAndView changePasswordView() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/changePassword");
		return mv;
	}
	
	/**
	 * 修改密码
	 */
	@ResponseBody
	@RequestMapping(value = "/system/admin/password/change/save")
	public ModelAndView changePasswordSave(@RequestParam(value = "password") String psw,@RequestParam(value = "newPassword") String newPsw) {
	    int id = UserContext.getLoginUser().getId();
		Integer res = adminService.changePassword(id,psw,newPsw);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/changePassword");
		String msg = PageTools.alertCloseFraRefresh("修改密码", res>0?"保存成功！":"保存失败，请稍后重试。", 1000);
		mv.addObject("actionMsg", msg);		
		return mv;
	}
}
